package com.cao.meet.swipelist.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.ListView;

import com.cao.meet.swipelist.MergeListAdapter;

/**
 * Created by small_world.
 * Date: 2018/9/11.
 * Time: 9:47.
 * purpose:
 */

public class CustomListView extends ListView {
    private MyLinearLayout mCurView;

//    private LinearLayout itemRoot;
//    private int mlastX = 0;
//    private final int MAX_WIDTH = 200;
    private Context mContext;

    public CustomListView(Context context, AttributeSet attrs) {
        super(context, attrs);
        mContext = context;
    }

//    @Override
//    public boolean onTouchEvent(MotionEvent event) {
//        int maxLength = dipToPx(mContext, MAX_WIDTH);
//
//        int x = (int) event.getX();
//        int y = (int) event.getY();
//
//        switch (event.getAction()) {
//            case MotionEvent.ACTION_DOWN: {
//                //我们想知道当前点击了哪一行
//                int position = pointToPosition(x, y);
//                if (position != INVALID_POSITION) {
//                    SwipeListActivity.MergeAdapter.DataHolder data = (SwipeListActivity.MergeAdapter.DataHolder) getItemAtPosition(position);
//                    itemRoot = data.rootView;
//                }
//            }
//            case MotionEvent.ACTION_MOVE: {
//                int scrollX = itemRoot.getScrollX();
//                int newScrollX = scrollX + mlastX - x;
//                if (newScrollX < 0) {
//                    newScrollX = 0;
//                } else if (newScrollX > maxLength) {
//                    newScrollX = maxLength;
//                }
//                itemRoot.scrollTo(newScrollX, 0);
//            }
//            break;
//            case MotionEvent.ACTION_UP: {
//                int scrollX = itemRoot.getScrollX();
//                int newScrollX = scrollX + mlastX - x;
//                if (scrollX > maxLength / 2) {
//                    newScrollX = maxLength;
//                } else {
//                    newScrollX = 0;
//                }
//                itemRoot.scrollTo(newScrollX, 0);
//            }
//            break;
//        }
//
//        mlastX = x;
//        return super.onTouchEvent(event);
//    }
//
//
//    private int dipToPx(Context context, int dip) {
//        return (int) (dip * context.getResources().getDisplayMetrics().density + 0.5f);
//    }

//    @Override
//    public boolean onTouchEvent(MotionEvent event) {
//        int x = (int) event.getX();
//        int y = (int) event.getY();
//
//        switch (event.getAction()) {
//            case MotionEvent.ACTION_DOWN: {
//                //我们想知道当前点击了哪一行
//                int position = pointToPosition(x, y);
//                if (position != INVALID_POSITION) {
//                    SwipeListActivity.MergeAdapter.DataHolder data = (SwipeListActivity.MergeAdapter.DataHolder) getItemAtPosition(position);
//                    mCurView = data.rootView;
//                }
//            }
//            break;
//            default:
//                break;
//        }
//        if (mCurView != null){
//            mCurView.disPatchTouchEvent(event);
//        }
//        return super.onTouchEvent(event);
//    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        int x = (int) event.getX();
        int y = (int) event.getY();

        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN: {
                //我们想知道当前点击了哪一行
                int position = pointToPosition(x, y);
                if (position != INVALID_POSITION) {
                    MergeListAdapter.DataHolder data = (MergeListAdapter.DataHolder) getItemAtPosition(position);
                    mCurView = data.rootView;
                }
            }
            break;
            default:
                break;
        }
        if (mCurView != null){
            mCurView.disPatchTouchEvent(event);
        }
        return super.onTouchEvent(event);
    }


}
